package edu.hawaii.senin.iclouds.util;

/**
 * Some methods to extend text manipulations.
 * 
 * @author Pavel Senin.
 * 
 */
public class TextUtils {

  /**
   * Mimics Perl chomp function by chomping leading and ending spaces and new line symbols along
   * with tab, etc.
   * 
   * @param line text line to chomp.
   * @return chomped line.
   */
  public static String chomp(String line) {
    StringBuilder res = new StringBuilder(line.length());
    // skip initial whitespaces, tabs, etc...
    Integer i = 0;
    while ((i < line.length()) && (!Character.isLetterOrDigit(line.charAt(i)))) {
      i++;
    }
    res.append(line.substring(i));
    i = res.length() - 1;
    while ((i > 0) && (!Character.isLetterOrDigit(res.charAt(i)))) {
      res.deleteCharAt(i);
      i--;
    }
    return res.toString();
  }
}
